import 'dart:async';
import 'package:flutter/foundation.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

Future<ResponseBody> fetchWeather() async {
  final client = http.Client();
  final response = await client.get('https://v2.jinrishici.com/info');

  return compute(parseWeather, response.body);
}

ResponseBody parseWeather(String responseBody) {
  print(responseBody);
  final parsed = jsonDecode(responseBody);
  return ResponseBody.fromJson(parsed);
}
class ResponseBody {
  String status;
  Data data;

  ResponseBody({this.status, this.data});

  ResponseBody.fromJson(Map<String, dynamic> json) {
    status = json['status'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    return data;
  }
}

class Data {
  String token;
  String ip;
  String region;
  WeatherData weatherData;
  List<String> tags;
  String beijingTime;

  Data(
      {this.token,
      this.ip,
      this.region,
      this.weatherData,
      this.tags,
      this.beijingTime});

  Data.fromJson(Map<String, dynamic> json) {
    token = json['token'];
    ip = json['ip'];
    region = json['region'];
    weatherData = json['weatherData'] != null
        ? new WeatherData.fromJson(json['weatherData'])
        : null;
    tags = json['tags'].cast<String>();
    beijingTime = json['beijingTime'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['token'] = this.token;
    data['ip'] = this.ip;
    data['region'] = this.region;
    if (this.weatherData != null) {
      data['weatherData'] = this.weatherData.toJson();
    }
    data['tags'] = this.tags;
    data['beijingTime'] = this.beijingTime;
    return data;
  }
}

class WeatherData {
  double temperature;
  String windDirection;
  int windPower;
  double humidity;
  String updateTime;
  String weather;
  String visibility;
  double rainfall;
  double pm25;

  WeatherData(
      {this.temperature,
      this.windDirection,
      this.windPower,
      this.humidity,
      this.updateTime,
      this.weather,
      this.visibility,
      this.rainfall,
      this.pm25});

  WeatherData.fromJson(Map<String, dynamic> json) {
    temperature = json['temperature'];
    windDirection = json['windDirection'];
    windPower = json['windPower'];
    humidity = json['humidity'];
    updateTime = json['updateTime'];
    weather = json['weather'];
    visibility = json['visibility'];
    rainfall = json['rainfall'];
    pm25 = json['pm25'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['temperature'] = this.temperature;
    data['windDirection'] = this.windDirection;
    data['windPower'] = this.windPower;
    data['humidity'] = this.humidity;
    data['updateTime'] = this.updateTime;
    data['weather'] = this.weather;
    data['visibility'] = this.visibility;
    data['rainfall'] = this.rainfall;
    data['pm25'] = this.pm25;
    return data;
  }
}